package com.zoo.diannaopeijian.service.impl;

import com.zoo.diannaopeijian.comm.sql.GeneratorExampleBuilder;
import com.zoo.diannaopeijian.domain.ExportItem;
import com.zoo.diannaopeijian.domain.ExportItemExample;
import com.zoo.diannaopeijian.mapper.ExportItemMapper;
import com.zoo.diannaopeijian.service.ExportItemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zx583 on 2018-01-30.
 */
@Service
public class ExportItemServiceImpl implements ExportItemService {
    @Resource
    private ExportItemMapper exportItemMapper;

    @Override
    public boolean insertExportItem(ExportItem exportItem) {
        return exportItemMapper.insertSelective(exportItem) == 1;
    }

    @Override
    public boolean insertExportItems(List<ExportItem> exportItemList) {
        for (ExportItem e : exportItemList) {
            if (!insertExportItem((e))) {
                return false;
            }
        }
        return true;
    }

    @Override
    public List getExportItemsByRecordId(Integer exportrecordid) {
        return exportItemMapper.getExportItemsByRecordId(exportrecordid);
    }

    @Override
    public List<ExportItem> deleteExportItems(ExportItem exportItem) {
        ExportItemExample example = (ExportItemExample) GeneratorExampleBuilder.buildEqualExample(ExportItemExample.class, exportItem, false);

        if (example == null) {
            throw new RuntimeException("当前正欲执行清空数表的非法操作");
        }

        List<ExportItem> itemBeDeleted = exportItemMapper.selectByExample(example);

        int numsBeDelete = exportItemMapper.deleteByExample(example);

        if (itemBeDeleted.size() != numsBeDelete) {
            // 要删除的数据被其他事务先删除了，要不要做什么东西呢
        }

        return itemBeDeleted;
    }
}
